﻿using System.Windows.Forms;
using System.Drawing;
using System.Resources;

namespace PMQLNhaHang.CustomControl
{
    public class exListBox : ListBox
    {
        #region old
        //private ImageList _myImageList;

        //public ImageList MyImageList
        //{
        //    get { return _myImageList; }
        //    set { _myImageList = value; }
        //}

        //public exListBox()
        //{
        //    ResourceManager rm = new ResourceManager(typeof(exListBox));
        //    this.DrawMode = DrawMode.OwnerDrawFixed;
        //    MyImageList = new ImageList();
        //    MyImageList.ImageSize = new Size(32, 32);
        //    MyImageList.Images.Add((System.Drawing.Image)rm.GetObject("Table_icon"));

        //}
        //protected override void OnDrawItem(DrawItemEventArgs e)
        //{

        //    ImgListboxItem item = null;
        //    Rectangle bounds = e.Bounds;
        //    Size imageSize = _myImageList.ImageSize;
        //    try
        //    {
        //        item = (ImgListboxItem)Items[e.Index];

        //        // = new Rectangle(0, e.Index * imageSize.Height, this.Bounds.Width, this.Bounds.Height);
        //        if (item != null)
        //        {
        //            if (item.ImageIndex != -1)
        //            {
        //                MyImageList.Draw(e.Graphics, bounds.Left, e.Index*imageSize.Height, item.ImageIndex);
        //                e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor), bounds.Left + imageSize.Width, e.Index * imageSize.Height+(imageSize.Height / 2 - e.Font.Height / 2));
        //            }
        //        }
        //    }
        //    catch
        //    {
        //        System.Console.Write("khong thanh cong");
        //    }
        //   // e.DrawBackground();
        //    //e.DrawFocusRectangle();
        //    base.OnDrawItem(e);
        //}
        //protected override void OnPaint(PaintEventArgs e)
        //{
        //    base.OnPaint(e);
        //}
        //protected override void OnPaintBackground(PaintEventArgs pevent)
        //{
        //    //pevent.Graphics.DrawRectangle(new Pen(Brushes.AntiqueWhite)), 0, 32, this.Bounds.Width, this.Bounds.Height);
        //    base.OnPaintBackground(pevent);
        //}
        #endregion
        private Size _imageSize;
        private StringFormat _fmt;
        private Font _titleFont;
        private Font _detailsFont;

        public exListBox(Font titleFont, Font detailsFont, Size imageSize,
                         StringAlignment aligment, StringAlignment lineAligment)
        {
            _titleFont = titleFont;
            _detailsFont = detailsFont;
            _imageSize = imageSize;
            this.ItemHeight = _imageSize.Height + this.Margin.Vertical;
            _fmt = new StringFormat();
            _fmt.Alignment = aligment;
            _fmt.LineAlignment = lineAligment;
            _titleFont = titleFont;
            _detailsFont = detailsFont;
        }

        public exListBox()
        {
            //InitializeComponent();
            _imageSize = new Size(32, 32);
            this.ItemHeight = _imageSize.Height + this.Margin.Vertical;
            _fmt = new StringFormat();
            _fmt.Alignment = StringAlignment.Near;
            _fmt.LineAlignment = StringAlignment.Near;
            _titleFont = new Font(this.Font, FontStyle.Bold);
            _detailsFont = new Font(this.Font, FontStyle.Regular);

        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (this.Items.Count > 0)
            {
                exListBoxItem item = (exListBoxItem)this.Items[e.Index];
                item.drawItem(e, this.Margin, _titleFont, _detailsFont, _fmt, this._imageSize);
            }
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
    }
}
